﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using ShareContent;

namespace AncientEmpires_025_132
{
    public class Building : VisibleGameEntity
    {
        // Tên của căn hộ: là nhà or là thành trì 
        protected BuildingName _myBuildingName;
        public BuildingName MyBuildingName
        {
            get { return _myBuildingName; }
            set { _myBuildingName = value; }
        }

        //Danh sách index trong ma trận các cell thuộc khu vực của building.
        private List<Vector2> _listIndexCellUse;
        public List<Vector2> ListIndexCellUse
        {
            get { return _listIndexCellUse; }
            set { _listIndexCellUse = value; }
        }

        // Tên của đội đang sở hữu building này.
        protected NationName _myNationName;
        public NationName MyNationName
        {
            get { return _myNationName; }
            set { _myNationName = value;
                    switch (_myNationName)
                    {
                        case NationName.None:
                            myIndexNation = 0;
                            break;
                        case NationName.Red:
                            myIndexNation = 1;
                            break;
                        case NationName.Green:
                            myIndexNation = 2;
                            break;
                        case NationName.Blue:
                            myIndexNation = 3;
                            break;
                        case NationName.Black:
                            myIndexNation = 4;
                            break;
                    }
                }
        }
        protected int myIndexNation;
        public int MyIndexNation
        {
            get { return myIndexNation; }
            set { myIndexNation = value; }
        }
        // Số tiền tăng lên trong 1 lượt của 1 căn nhà cho đội nào chiếm được
        protected int _increaseMoney;
        public int IncreaseMoney
        {
            get { return _increaseMoney; }
            set { _increaseMoney = value; }
        }

        public delegate void EventHandleSelected();
        public event EventHandleSelected EventSelected;

        public delegate void EventHandUnselected( Building building,Vector2 position);
        public event EventHandUnselected EventUnSelected;


        private bool _isSelected;
        public bool IsSelected
        {
            get { return _isSelected; }
            set { _isSelected = value; }
        }

        private int _numberOfClick = 0;
        public int NumberOfClick
        {
            get { return _numberOfClick; }
            set { _numberOfClick = value; }
        }

        public override void UpdateMouseState()
        {
            MouseState ms = Mouse.GetState();
            if (this.MyNationName == CurrentCircle.NationCurrent)
            {
                if (ms.RightButton == ButtonState.Pressed)
                {
                    if (IsSelected)
                    {
                        NumberOfClick = 0;
                        IsSelected = false;
                        EventUnSelected(this, new Vector2(ms.X, ms.Y));
                    }
                }
                if (ms.LeftButton == ButtonState.Pressed)
                {
                    if ((NumberOfClick == 0 && CheckClickSprite(new Vector2(ms.X, ms.Y))) || NumberOfClick > 0)
                    {
                        if (PreMouseState == ButtonState.Released)
                        {
                            PreMouseState = ButtonState.Pressed;
                            NumberOfClick = (NumberOfClick + 1) % 3;
                        }
                    }
                }
                else
                {
                    if (CheckHoverSpirte(new Vector2(ms.X, ms.Y)))
                    {
                        IsHover = true;
                    }
                    else
                    {
                        IsHover = false;
                    }
                    if (PreMouseState == ButtonState.Pressed)
                    {
                        PreMouseState = ButtonState.Released;
                    }
                }

                switch (NumberOfClick)
                {
                    case 1:
                        {
                            if (!IsSelected && PreMouseState == ButtonState.Released)
                            {
                                IsSelected = true;
                                EventSelected();
                            }
                            break;
                        }

                    case 2: // Bo chon 
                        {
                            if (IsSelected)
                            {
                                IsSelected = false;
                                EventUnSelected(this, new Vector2(ms.X, ms.Y));
                                NumberOfClick = 0;
                            }
                            break;
                        }
                }
            }
            else
            {
                IsHover = false;
            }
            
        }

        public virtual void SetListIndexCellUse(Vector2 indexLimit, Vector2 indexMatrix, Vector2 multiple)
        {
            for (int i = 0; i < multiple.X; i++)
            {
                for (int j = 0; j < multiple.Y; j++)
                {
                    if (indexMatrix.X + i < indexLimit.X && indexMatrix.Y + j < indexLimit.Y)
                    {
                        ListIndexCellUse.Add(new Vector2(indexMatrix.X + i, indexMatrix.Y + j));
                    }
                }
            }
        }
        public Character NewCharacter(ContentManager content, CharacterName name, Vector2 position, Vector2 topleft, NationName nationname)
        {
            Character temp = null;
            switch (name)
            {
                case CharacterName.King:
                    temp = new KingCharacter(content, position, topleft, nationname);
                    break;
                case CharacterName.Mage:
                    temp = new MageCharacter(content, position, topleft, nationname);
                    break;
                case CharacterName.Soldier:
                    temp = new SoldierCharacter(content, position, topleft, nationname);
                    break;
                case CharacterName.Hydra:
                    temp = new HydraCharacter(content, position, topleft, nationname);
                    break;
                case CharacterName.Griffin:
                    temp = new GriffinCharacter(content, position, topleft, nationname);
                    break;
                case CharacterName.Fairy:
                    temp = new FairyCharacter(content, position, topleft, nationname);
                    break;
                case CharacterName.Combative:
                    temp = new CombativeCharacter(content, position, topleft, nationname);
                    break;
                case CharacterName.Cavalry:
                    temp = new CavalryCharacter(content, position, topleft, nationname);
                    break;
                case CharacterName.Archer:
                    temp = new ArcherCharacter(content, position, topleft, nationname);
                    break;
            }
            //for (int i = 0; i < temp.ListSprites.Count; i++)
            //{
            //    temp.ListSprites[i].TopLeft = -temp.ListSprites[i].Position;
            //}
            return temp;
        }
        public override void Update(GameTime gameTime)
        {
            //for (int i = 0; i < ListSprites.Count; i++)
            //{
            //    ListSprites[i].Position = -ListSprites[i].TopLeft;
            //}
            base.Update(gameTime);
        }
        
        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            //for (int i = 0; i < ListSprites.Count; i++)
            //{
            //    Rectangle rect = new Rectangle();
            //    if (ListSprites[i].Size.X == 0 && ListSprites[i].Size.Y == 0)
            //    {
            //        ListSprites[i].Size = new Vector2(ListSprites[i].Textures[ListSprites[i].ITexture].Width, ListSprites[i].Textures[ListSprites[i].ITexture].Height);
            //    }
            //    rect = new Rectangle((int)ListSprites[i].Position.X, (int)ListSprites[i].Position.Y, (int)ListSprites[i].Size.X, (int)ListSprites[i].Size.Y);
            //    spriteBatch.Draw(ListSprites[i].Textures[ListSprites[i].ITexture], rect, Color.White);
            //}
            int i = ISprite;
            Rectangle rect = new Rectangle();
            if (ListSprites[i].Size.X == 0 && ListSprites[i].Size.Y == 0)
            {
                ListSprites[i].Size = new Vector2(ListSprites[i].Textures[ListSprites[i].ITexture].Width, ListSprites[i].Textures[ListSprites[i].ITexture].Height);
            }
            rect = new Rectangle((int)ListSprites[i].Position.X, (int)ListSprites[i].Position.Y, (int)ListSprites[i].Size.X, (int)ListSprites[i].Size.Y);
            Color color = new Color();
            switch (MyNationName)
            {
                case NationName.Red:
                    color = new Color(200, 50, 50);
                    break;
                case NationName.Green:
                    color = new Color(50, 200, 50);
                    break;
                case NationName.Blue:
                    color = new Color(50, 50, 200);
                    break;
                case NationName.Black:
                    color = new Color(50, 50, 50);
                    break;
                case NationName.None:
                    color = new Color(100, 100, 100);
                    break;
            }
            spriteBatch.Draw(ListSprites[i].Textures[ListSprites[i].ITexture], rect, color * 0.7f);
                
            
        }

        /*
        //Change character after nation color
        public void SetColorAfterNation(List<PixelColor> listBeChangeColor, NationName nationName)
        {
            List<Color> listColor = new List<Color>();
            for (int i = 0; i < listBeChangeColor.Count; i++)
            {
                Color temp = new Color(listBeChangeColor[i].R, listBeChangeColor[i].G, listBeChangeColor[i].B);
                
                listColor.Add(temp);
            }
            for (int i = 0; i < ListSprites.Count; i++)
            {
                for (int j = 0; j < ListSprites[i].NTextures; j++)
                {
                    Texture2D changeTexture = ListSprites[i].Textures[j];
                    changeTexture = SetTextureColorAfterNation(listColor, changeTexture, nationName);
                    ListSprites[i].Textures[j] = changeTexture;
                }
            }
        }
        
        //Change color texture after nation color
        public Texture2D SetTextureColorAfterNation(List<Color> listBeChangeColor, Texture2D changeTexture, NationName nationName)
        {
            Color[] data;
            data = new Color[changeTexture.Width * changeTexture.Height];
            changeTexture.GetData(data);
            for (int i = 0; i < data.Length; i++)
            {
                for (int j = 0; j < listBeChangeColor.Count; j++)
                {
                    if (data[i] == listBeChangeColor[j])
                    {
                        int b = 0, r = 0, g = 0;
                        switch (nationName)
                        {
                            case NationName.Blue:
                                b = (int)MathHelper.Max(MathHelper.Max(listBeChangeColor[j].R, listBeChangeColor[j].B), listBeChangeColor[j].G);
                                r = (int)MathHelper.Min(MathHelper.Min(listBeChangeColor[j].R, listBeChangeColor[j].B), listBeChangeColor[j].G);
                                if (listBeChangeColor[j].R < b && listBeChangeColor[j].R > r) g = listBeChangeColor[j].R;
                                else if (listBeChangeColor[j].B < b && listBeChangeColor[j].B > r) g = listBeChangeColor[j].B;
                                else if (listBeChangeColor[j].G < b && listBeChangeColor[j].G > r) g = listBeChangeColor[j].G;
                                break;
                            case NationName.Green:
                                g = (int)MathHelper.Max(MathHelper.Max(listBeChangeColor[j].R, listBeChangeColor[j].B), listBeChangeColor[j].G);
                                b = (int)MathHelper.Min(MathHelper.Min(listBeChangeColor[j].R, listBeChangeColor[j].B), listBeChangeColor[j].G);
                                if (listBeChangeColor[j].R < g && listBeChangeColor[j].R > b) r = listBeChangeColor[j].R;
                                else if (listBeChangeColor[j].B < g && listBeChangeColor[j].B > b) r = listBeChangeColor[j].B;
                                else if (listBeChangeColor[j].G < g && listBeChangeColor[j].G > b) r = listBeChangeColor[j].G;
                                break;
                            case NationName.Red:
                                r = (int)MathHelper.Max(MathHelper.Max(listBeChangeColor[j].R, listBeChangeColor[j].B), listBeChangeColor[j].G);
                                b = (int)MathHelper.Min(MathHelper.Min(listBeChangeColor[j].R, listBeChangeColor[j].B), listBeChangeColor[j].G);
                                if (listBeChangeColor[j].R < r && listBeChangeColor[j].R > b) g = listBeChangeColor[j].R;
                                else if (listBeChangeColor[j].B < r && listBeChangeColor[j].B > b) g = listBeChangeColor[j].B;
                                else if (listBeChangeColor[j].G < r && listBeChangeColor[j].G > b) g = listBeChangeColor[j].G;
                                break;
                            case NationName.Black:
                                r = (int)Math.Sqrt(listBeChangeColor[j].R);
                                b = (int)Math.Sqrt(listBeChangeColor[j].B);
                                g = (int)Math.Sqrt(listBeChangeColor[j].G);
                                break;
                        }
                        data[i] = new Color(r, b, g);
                        break;
                    }
                }
            }
            changeTexture.SetData(data);
            return changeTexture;
        }

        */
    }
}
